/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import models.Course;
import models.ForumAnswer;
import models.ForumQuestion;
import models.User;
import org.hibernate.Session;
import service.CourseDao;
import service.ForumDao;
import service.HibernateUtil;
import service.UserDao;

/**
 *
 * @author kjeld
 */
public class ForumQuestionController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        boolean loggedin = false;
        long userID = 0;
        for (Cookie cookie : request.getCookies()) {
            if (cookie.getName().equals("status")) {
                if (cookie.getValue().length() > 0) {
                    loggedin = true;
                }
            } else if (cookie.getName().equals("userID")) {
                userID = Long.parseLong(cookie.getValue());
            }
        }
        if (loggedin == true) {
            HttpSession session = request.getSession(true);
            HibernateUtil HU = new HibernateUtil();
            Session sessie = HU.openSession();

            try {
                long courseID = 0;
                if (request.getParameter("courseID") != null) {
                    courseID = Long.parseLong(request.getParameter("courseID"));
                }
                CourseDao cdao = new CourseDao(sessie);
                if (courseID == 0) {
                    response.sendRedirect("/");
                } else {
                    Course course = cdao.get(courseID);
                    request.setAttribute("course", course);
                    Set<ForumQuestion> questions = course.getForumQuestions();
                }
                RequestDispatcher rd = request.getRequestDispatcher("/forumpost.jsp");
                rd.forward(request, response);
            } catch (Exception exception) {
                System.out.println(exception);
            }
        } else {
            response.sendRedirect("/");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("postpost");
        long userID = 0;
        for (Cookie cookie : request.getCookies()) {
            if (cookie.getName().equals("userID")) {
                userID = Long.parseLong(cookie.getValue());
            }
        }
        long courseID = 0;

        HttpSession session = request.getSession(true);
        HibernateUtil HU = new HibernateUtil();
        Session sessie = HU.openSession();
        CourseDao cdao = new CourseDao(sessie);

        Course course = null;
        
        if (request.getParameter("courseID") != null) {
            System.out.println("Course: " + request.getParameter("courseID"));
            courseID = Long.parseLong(request.getParameter("courseID").toString());
            course = cdao.get(courseID);
        }
        if (course != null) {
            System.out.println("Course: " + course.getName());
            Set<ForumQuestion> questions = course.getForumQuestions();
            ForumQuestion question = null;
            Date date = new Date();
            question = new ForumQuestion(request.getParameter("title"), request.getParameter("description"), date);
            UserDao udao = new UserDao(sessie);
            User user = udao.get(userID);
            question.setQuestionPoster(user);
            ForumDao fdao = new ForumDao(sessie);
            fdao.save(question);
            course.addForumQuestion(question);
            cdao.update(course);
        }
        response.sendRedirect("/forum?courseID="+course.getCourseID());
    }
}
